Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
Proceeding conditionally to the next mergeMap? [ rxjs.Observable]

I'd like to convert the my nested Observables with the use of pipe,map,mergeMap, however there are conditions after which I want it to proceed

observableFunction is an Observable that loops through an array, once its finished you can move on to use the manipulated data

observableFunction(par1,par2)
.subscribe((resp) => {
  i++;
 //do stuff, manipulate resp   
  const lastIteration = someArray.length == i;

  if (lastIteration) {
   //do stuff
   observableFunction(par1a, par2a)
      .subscribe((resp) => {
        observableHandleFunction(resp)
          .subscribe((handledData) => {
            h++;
            const lastIterationSub = someNestedArray.length == h;

            //do stuff, manipulate handledData

            if (lastIterationSub) {
              console.log("done");

          });
      });
  }
});

How can I convert so that it does seem more easy to read by using the features of rxjs that are meant to be used in this scenario? Like applying conditions to mergeMap?

observableFunction(par1,par2)
 .pipe(
    mergeMap((res1) => {
       //do stuff
       CONDITION={
         return observableFunction(par1a,par2a)
       }
    }),
    mergeMap((res2) => return observableHandleFunction(res2))
 )
 .subscribe((res3) => {
        h++;
        //do stuff
        const lastIterationSub = someNestedArray.length == h;

        if (lastIterationSub) {
          console.log("done");

        }
});

With a solution something like this its not gonna work with the condition, leading to an error

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

First of all, nesting subscribe blocks is always a bad idea.

If your observableFunction emits single values (rather than a whole array at once) and when its done it emits a complete value, you can use toArray operator. So:

observableFunction(par1,par2)
.pipe(toArray())
// Here you will have an array of items, now you can proceed with something like:

observableFunction(par1,par2)
  .pipe(
    toArray(),
    mergeMap(array => {
    // If neccesary you can do the same here etc
    }))
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda